package com.aiobc.file.enums;

import org.springframework.util.StringUtils;

/**
 * 文件格式类型枚举
 * 
 * @author wangjun18 2020/3/14
 */
public enum FileEnum {
    JPG("image", ".jpg"), JPEG("image", ".jpeg"), BMP("image", ".bmp"), GIF("image", ".gif"), PNG("image", ".png"),
    MP4("video", ".mp4"), RM("video", ".rm"), RMVB("video", ".rmvb"), AVI("video", ".avi"), THIRD_GP("video", ".3gp"),
    MKV("video", ".mkv"), DOCX("word", ".docx"), DOC("word", ".doc"), XLSX("excel", ".xlsx"), XLS("excel", ".xls"),
    PDF("pdf", ".pdf");

    /**
     * 文件类型代码
     */
    private String type;

    /**
     * 文件后缀名
     */
    private final String suffix;

    FileEnum(String type, String suffix) {
        this.type = type;
        this.suffix = suffix;
    }

    public String getCode() {
        return type;
    }

    public String getSuffix() {
        return suffix;
    }

    public static FileEnum parseType(String type) {
        if (StringUtils.isEmpty(type)) {
            return null;
        }

        for (FileEnum fileEnum : values()) {
            if (fileEnum.type.equals(type)) {
                return fileEnum;
            }
        }

        return null;
    }

    public static FileEnum parse(String suffix) {
        if (StringUtils.isEmpty(suffix)) {
            return null;
        }

        for (FileEnum fileEnum : values()) {
            if (fileEnum.suffix.equals(suffix)) {
                return fileEnum;
            }
        }

        return null;
    }

    /**
     * 是否是图片
     * 
     * @return
     */
    public boolean isImage() {
        return this == JPG || this == JPEG || this == BMP || this == GIF || this == PNG;
    }

    /**
     * 是否是视频
     * 
     * @return
     */
    public boolean isVideo() {
        return this == MP4 || this == RM || this == RMVB || this == AVI || this == THIRD_GP || this == MKV;
    }

    /**
     * 是否是excel
     * 
     * @return
     */
    public boolean isExcel() {
        return this == XLS || this == XLSX;
    }

    /**
     * 是否是word
     * 
     * @return
     */
    public boolean isWord() {
        return this == DOC || this == DOCX;
    }
}
